package com.example.tool.entity;

import com.example.tool.module.ModuleInformation;
import com.example.tool.utils.AES;

public class ConfigEntity {
  // 项目根路径
  private String projectPath;
  // 插件ID
  private String pluginID;
  // 编译路径
  private String buildPath;
  // 项目同步插件的ID
  private String sshID;
  // 源代码目录 用于指定创建PO实体类的
  private String sourcePath;

  public ConfigEntity(String projectPath, String pluginID, String buildPath, String sshID, String sourcePath) {
    this.projectPath = projectPath;
    this.pluginID = pluginID;
    this.buildPath = buildPath;
    this.sshID = sshID;
    this.sourcePath = sourcePath;
  }

  private ConfigEntity(String[] config) {
    if (config == null) {
      this.projectPath = null;
      this.pluginID = null;
      this.buildPath = null;
      this.sshID = null;
      this.sourcePath = null;
    } else {
      this.projectPath = config[0];
      this.pluginID = config[1];
      this.buildPath = config[2];
      this.sshID = config[3];
      this.sourcePath = config[4];
    }
  }

  public static ConfigEntity init(String base) throws Exception {
    if (!base.isEmpty()) {
      String decrypt = AES.decrypt(ModuleInformation.MODULE_SECRET_KEY, base);
      String[] split = new String[5];
      String[] strings = decrypt.split("\\|");
      for (int i = 0; i < split.length; i++) {
        if (i < strings.length) {
          split[i] = strings[i];
        } else {
          split[i] = null;
        }
      }
      return new ConfigEntity(split);
    }
    return null;
  }

  public String finalResult() throws Exception {
    return AES.encryption(ModuleInformation.MODULE_SECRET_KEY, this.toString());
  }

  public String getProjectPath() {
    return projectPath;
  }

  public String getPluginID() {
    return pluginID;
  }

  public String getBuildPath() {
    return buildPath;
  }

  public String getSshID() {
    return sshID;
  }

  public String getSourcePath() {
    return sourcePath;
  }

  public void setProjectPath(String projectPath) {
    this.projectPath = projectPath;
  }

  public void setPluginID(String pluginID) {
    this.pluginID = pluginID;
  }

  public void setBuildPath(String buildPath) {
    this.buildPath = buildPath;
  }

  public void setSshID(String sshID) {
    this.sshID = sshID;
  }

  public void setSourcePath(String sourcePath) {
    this.sourcePath = sourcePath;
  }

  @Override
  public String toString() {
    return projectPath + "|" + pluginID + "|" + buildPath + "|" + sshID + "|" + sourcePath;
  }
}
